package com.gm.lib.data;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.gm.common.context.GlobalContext;
import com.gm.lib.net.RequestConfig;


/**
 * 设置存储类
 *
 * @author haifei.zhang
 * @date 2013-9-8
 */
public class SettingPreference {

    Context mContext;
    SharedPreferences mSharedPreferences;

    private static final String fileName = "setting_preference";

    private String KEY_Everyday_Remind = "KEY_Everyday_Remind";
    private String KEY_Is_First_Launch = "KEY_Is_first_launch";
    private String KEY_SHOW_APP_INTRODUCE = "KEY_SHOW_APP_INTRODUCE" + 2;//如果有修改需要版本号+1
    private String KEY_BASE_URL = "KEY_BASE_URL";

    private static SettingPreference mCartPreference;

    public static SettingPreference getInstance() {
        if (mCartPreference == null) {
            mCartPreference = new SettingPreference(GlobalContext.getContext());
        }
        return mCartPreference;
    }

    private SettingPreference(Context context) {
        mContext = context;
        mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }

    /**
     * 设置定时提醒
     *
     * @param value
     */
    public void setEverydayRemind(boolean value) {
        Editor e = mSharedPreferences.edit();
        e.putBoolean(KEY_Everyday_Remind, value);
        e.commit();
    }

    /**
     * 得到定时提醒状态
     *
     * @return
     */
    public boolean getEverydayRemind() {
        return mSharedPreferences.getBoolean(KEY_Everyday_Remind, true);
    }

    /**
     * 设置 第一次启动标志
     *
     * @param value
     */
    public void setFirstLaunch(boolean value) {
        Editor e = mSharedPreferences.edit();
        e.putBoolean(KEY_Is_First_Launch, value);
        e.commit();
    }

    /**
     * 是否是第一次启动
     *
     * @return
     */
    public boolean isFirstLaunch() {
        return mSharedPreferences.getBoolean(KEY_Is_First_Launch, true);
    }

    /**
     * 设置 是否已经看过app引导页
     *
     * @param value
     */
    public void setShowAppIntroduce(boolean value) {
        Editor e = mSharedPreferences.edit();
        e.putBoolean(KEY_SHOW_APP_INTRODUCE, value);
        e.commit();
    }

    /**
     * 是否显示app引导页
     *
     * @return
     */
    public boolean isShowAppIntroduce() {
        return mSharedPreferences.getBoolean(KEY_SHOW_APP_INTRODUCE, true);
    }

    public void clearSharedPrefs(Context context) {
        Editor e = mSharedPreferences.edit();
        e.clear();
        e.commit();
    }


    /**
     * 设置URL
     *
     * @param value
     */
    public void setBaseUrl(String value) {
        Editor e = mSharedPreferences.edit();
        e.putString(KEY_BASE_URL, value);
        e.commit();
    }

    /**
     * 得到URL
     *
     * @return
     */
    public String getBaseUrl() {
        String url = mSharedPreferences.getString(KEY_BASE_URL, RequestConfig.DEFAULT_URL);
        if (url == null || url.length() < 1) {
            return RequestConfig.DEFAULT_URL;
        } else {
            return url;
        }
    }
}
